// (c) 2015 Teresa Holfeld
package com.holfeld.teresa.vocabtrainer;

import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;

import com.holfeld.teresa.vocabtrainer.provider.Vocabulary;
import com.holfeld.teresa.vocabtrainer.provider.VocabularyPool;


public class VocableListActivity extends ActionBarActivity {

    private static final String STATE_VOCABULARY = "StateVocabulary";
    private static final int EDIT_VOCABULARY_REQUEST = 0;

    private Vocabulary mVocabulary;

    private TextView mLocaleTextView;
    private ListView mVocableListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vocable_list);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mLocaleTextView = (TextView) findViewById(R.id.vocabularyInfo);

        String vocabularyName = getIntent().getStringExtra(TrainVocabularyFragment.ARG_VOCAB_NAME);
        updateVocables(vocabularyName);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putString(STATE_VOCABULARY, mVocabulary.getName());
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_vocable_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == android.R.id.home) {
            Intent goBackUpIntent = new Intent(this, TrainVocabularyActivity.class);
            goBackUpIntent.putExtra(TrainVocabularyFragment.ARG_VOCAB_NAME, mVocabulary.getName());
            NavUtils.navigateUpTo(this, goBackUpIntent);
            return true;
        } else if (id == R.id.action_edit_stack) {
            Intent editStackIntent = new Intent(this, EditVocabularyActivity.class);
            editStackIntent.putExtra(TrainVocabularyFragment.ARG_VOCAB_NAME, mVocabulary.getName());
            startActivityForResult(editStackIntent, EDIT_VOCABULARY_REQUEST);
        }
        else if (id == R.id.action_edit_word) {
            Intent editWordIntent = new Intent(this, EditVocableActivity.class);
            editWordIntent.putExtra(TrainVocabularyFragment.ARG_VOCAB_NAME, mVocabulary.getName());
            startActivity(editWordIntent);
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == EDIT_VOCABULARY_REQUEST) {
            String vocabulary = getIntent().getStringExtra(TrainVocabularyFragment.ARG_VOCAB_NAME);
            updateVocables(vocabulary);
//            if (resultCode == RESULT_OK) {
//                updateVocables(String vocabularyName);
//            }
        }

    }

    private void updateVocables(String vocabularyName) {
        mVocabulary = VocabularyPool.getInstance(this).getVocabulary(vocabularyName);
        setTitle(vocabularyName);
        mLocaleTextView.setText(mVocabulary.getLocale1() + " - " + mVocabulary.getLocale2());
        mVocableListView = (ListView) findViewById(R.id.vocableList);
        mVocableListView.setAdapter(new VocableArrayAdapter(this, mVocabulary.getVocablePairs()));
    }
}
